<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class Prefs_model extends MY_Model {

    protected $pref;

    function __construct(){
        parent::__construct();
    }

    function table_name(){
        return "prefs";
    }

    function get_prefs($filters = array(),$counting = false){
        if (isset($this->CI->cache)){
            $caching = true;
            $cache_key = 'ci--PREF_'.md5(serialize($filters));
            if ($counting){
                $cache_key = 'ci--PREF_COUNT_'.md5(serialize($filters));
            }
            if ($return = $this->cache->file->get($cache_key)) {
                return ($return == 'empty_cache') ? FALSE : $return;
            }
        }else{
            $caching = false;
        }

        $this->db->select('prefs.*, regions.`name` as region_name');
        $this->db->from('prefs');
        $this->db->join('regions', 'prefs.region_id = regions.region_id');

        $this->db->order_by('prefs.pref_id','DESC');

        $param = array();

        if (isset($filters['pref_id'])) {
            $this->db->where('prefs.pref_id', $filters['pref_id']);
        }
        if (isset($filters['name'])) {
            $this->db->like('prefs.name', $filters['name']);
        }
        if (isset($filters['alias'])) {
            $this->db->where('prefs.alias', $filters['alias']);
        }
        if (isset($filters['region_id'])) {
            $this->db->where('prefs.region_id', $filters['region_id']);
        }
        if (isset($filters['status'])) {
            $this->db->where('prefs.status', $filters['status']);
        }

        if (isset($filters['limit'])) {
            $offset = (isset($filters['page'])) ? $filters['page'] : 0;
            $this->db->limit($filters['limit'], $offset);
        }

        $result = $this->db->get();

        if($counting !=false){
            $rows = $result->num_rows();
            $result->free_result();

            if ($caching == TRUE) {
                $this->CI->cache->file->save($cache_key, $rows, (5*60));
            }

            return $rows;
        }
        if ($result->num_rows() == 0) {
            return false;
        }
        $pref = array();
        foreach ($result->result_array() as $row ){
            $row['link'] = create_url('pref/'.$row['alias'],array('rewrite'=>true));
            $pref[] = $row;
        }
        if ($caching){
            $this->CI->cache->file->save($cache_key,$pref);
        }
        $result->free_result();

        return $pref;
    }

    function get_count($filter = array()){
        return $this->get_prefs($filter,true);
    }

    function get_pref($prefId){
        if (!isset($this->pref[$prefId])){
            $pref = $this->get_prefs(array('pref_id'=>$prefId));
            $this->pref[$prefId] = $pref;
        }else{
            $pref = $this->pref[$prefId];
        }
        if (empty($pref)) {
            return false;
        }
        return $pref[0];
    }

    function insert_pref($data){
        $_pref_id = $this->save($data);
        return $_pref_id;
    }

    function update_pref($data, $pref_id){
        return $this->update($data, array('pref_id' => $pref_id));
    }

    function delete_pref($pref_id) {
        return $this->delete(array('pref_id'=>$pref_id));
    }

    function change_status_pref($pref_id) {
        $status = $this->db->select('status')->from('prefs')->where('pref_id', $pref_id)->get()->result_array();

        if ($status[0]['status'] == 1) {
            $change = 0;
        } else {
            $change = 1;
        }
        return $this->update(array('status' => $change), array('pref_id' => $pref_id));
    }

    function get_pref_by_alias($pref_alias){
        $pref = $this->get_prefs(array('alias'=>$pref_alias),false);
        if ($pref){
            return $pref[0];
        }
        return false;
    }

    function get_prefs_options($filters=array()){
        $prefs_options = array();
        $prefs = $this->get_prefs($filters);
        if (is_array($prefs) && count($prefs)){
            foreach ($prefs as $pref){
                $prefs_options[$pref['pref_id']] = $pref['name'];
            }
        }
        return $prefs_options;
    }
}
?>